package org.jaaksi.windowanim;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;
import android.view.*;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MyActivity extends Activity {
	private WindowManager windowManager;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		windowManager = (WindowManager) this
			.getSystemService(Context.WINDOW_SERVICE);
		Display display = windowManager.getDefaultDisplay();
		int screenWidth = display.getWidth();
		int screenHeight = display.getHeight();

	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		Log.i("fcy", "----------------- fcy");
	}

	public void show(View view) {
		Display display = windowManager.getDefaultDisplay();
		int screenWidth = display.getWidth();
		int screenHeight = display.getHeight();

		View root = getLayoutInflater().inflate(R.layout.anim, null);
		root.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(MyActivity.this, "别点了", Toast.LENGTH_SHORT).show();
			}
		});

		ImageView imageView = (ImageView) root.findViewById(R.id.imageView);
		AnimationDrawable anim = (AnimationDrawable) imageView.getDrawable();

		final WindowManager.LayoutParams params = new WindowManager.LayoutParams();

		// 类型
		params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

		// FLAG_NOT_TOUCH_MODAL 不阻塞事件传递到后面的窗口
		// 设置 FLAG_NOT_FOCUSABLE 悬浮窗口较小时，后面的应用图标由不可长按变为可长按
		// 不设置这个flag的话，home页的划屏会有问题
		// 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE，弹出的View收不到Back键的事件；
		int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
//			| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
		params.flags = flags;

//		params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
//			| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
//			| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;

		//强制竖屏
		params.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

		// 根据电话状态调整透明窗位置
		params.type = WindowManager.LayoutParams.TYPE_PHONE;
		// 设置图片格式，效果为背景透明
//		params.format = PixelFormat.RGBA_8888;

		// 不设置这个弹出框的透明遮罩显示为黑色
		params.format = PixelFormat.TRANSPARENT;
//		params.alpha = 0.8f;
		params.alpha = 1;

//		params.width = WindowManager.LayoutParams.MATCH_PARENT;
//		params.height = WindowManager.LayoutParams.MATCH_PARENT;
		params.width = 600;
		params.height = 800;
		params.gravity = Gravity.CENTER;

		root.setOnTouchListener(new WindowTouchListener(params));
		windowManager.addView(root, params);
		anim.start();
	}

	class WindowTouchListener implements View.OnTouchListener {
		private int lastX, lastY;
		private int paramX, paramY;
		private WindowManager.LayoutParams params;

		public WindowTouchListener(WindowManager.LayoutParams params) {
			this.params = params;
		}

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					lastX = (int) event.getRawX();
					lastY = (int) event.getRawY();
					paramX = params.x;
					paramY = params.y;
					break;
				case MotionEvent.ACTION_MOVE:
					int dx = (int) event.getRawX() - lastX;
					int dy = (int) event.getRawY() - lastY;
					params.x = paramX + dx;
					params.y = paramY + dy;
					// 更新悬浮窗位置
					windowManager.updateViewLayout(v, params);
					break;
			}
			return false;
		}
	}
}
